<!doctype html>
<html>
    <head>
        <title>Set/Release capture</title>
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type="text/css" href="pointerevent_styles.css">
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <!-- Additional helper script for common checks across event types -->
        <script type="text/javascript" src="pointerevent_support.js"></script>
    </head>
    <body>
        <h1>Pointer Events capture test</h1>
        <h4>
            Test Description: This test checks if setCapture/releaseCapture functions works properly. Complete the following actions:
            <ol>
                <li> Put your mouse over the black rectangle. pointerover and pointerenter should be logged inside of it.</li>
                <li> Move your mouse out of the black rectangle. pointerout and pointerleave should be logged inside of it</li>
                <li> Put your mouse over the purple rectangle. pointerover and pointerenter should be logged inside of it.</li>
                <li> Move your mouse out of the purple rectangle. pointerout and pointerleave should be logged inside of it</li>
                <li> Press and hold left mouse button over "Set Capture" button. "gotpointercapture" should be logged in the black rectangle</li>
                <li> Put your mouse over the purple rectangle and then move it out. Nothing should happen</li>
                <li> Put your mouse over the black rectangle. pointerover and pointerenter should be logged inside of it.</li>
                <li> Move your mouse out of the black rectangle. pointerout and pointerleave should be logged inside of it</li>
                <li> Release left mouse button. "lostpointercapture" should be logged in the black rectangle</li>
            </ol>
        </h4>
        Test passes if the proper behaviour of the events is observed.
        <div id="target0"></div>
        <br>
        <div id="target1"></div>
        <br>
        <input type="button" id="btnCapture" value="Set Capture">
        <script type='text/javascript'>
            var isPointerCapture = false;
            var isRelatedTargetValueTested = false;
            var count = 0;

            var detected_pointertypes = {};
            add_completion_callback(showPointerTypes);

            var target0 = document.getElementById('target0');
            var target1 = document.getElementById('target1');
            var captureButton = document.getElementById('btnCapture');

            var test_gotpointercapture = async_test("gotpointercapture event received");
            var test_lostpointercapture = async_test("lostpointercapture event received");

            var test_pointerover_no_capture = async_test("pointerover event without capture received");
            var test_pointerover_capture = async_test("pointerover event with capture received");

            var test_pointerout_no_capture = async_test("pointerout event without capture received");
            var test_pointerout_capture = async_test("pointerout event with capture received");

            var test_pointerenter_no_capture = async_test("pointerenter event without capture received");
            var test_pointerenter_capture = async_test("pointerenter event with capture received");

            var test_pointerleave_no_capture = async_test("pointerleave event without capture received");
            var test_pointerleave_capture = async_test("pointerleave event with capture received");

            window.onload = function() {
                on_event(captureButton, 'pointerdown', function(e) {
                    if(isPointerCapture == false) {
                        sPointerCapture(e);
                        isPointerCapture = true;
                    }
                });

                on_event(target0, 'gotpointercapture', function(e) {
                    test_gotpointercapture.done();
                    log("gotpointercapture", target0);
                });

                on_event(target0, 'lostpointercapture', function(e) {
                    isPointerCapture = false;
                    test_lostpointercapture.done();
                    log("lostpointercapture", target0);
                });

                run();
            }

            function run() {
                on_event(target0, "pointerover", function (event) {
                    detected_pointertypes[ event.pointerType ] = true;
                    log("pointerover", target0);
                    if(isPointerCapture) {
                        test_pointerover_capture.done();
                        if (!isRelatedTargetValueTested) {
                            test(function() {
                                assert_true(event.relatedTarget==null, "relatedTarget is null when the capture is set")
                            }, "relatedTarget is null when the capture is set. relatedTarget is " + event.relatedTarget);
                            isRelatedTargetValueTested = true;
                        }
                    }
                    else {
                        test_pointerover_no_capture.done();
                    }
                });

                on_event(target0, "pointerout", function (event) {
                    log("pointerout", target0);
                    if(isPointerCapture) {
                        test_pointerout_capture.done();
                    }
                    else {
                        test_pointerout_no_capture.done();
                    }
                });

                on_event(target0, "pointerenter", function (event) {
                    log("pointerenter", target0);
                    if(isPointerCapture) {
                        test_pointerenter_capture.done();
                    }
                    else {
                        test_pointerenter_no_capture.done();
                    }
                });

                on_event(target0, "pointerleave", function (event) {
                    log("pointerleave", target0);
                    if(isPointerCapture) {
                        test_pointerleave_capture.done();
                    }
                    else {
                        test_pointerleave_no_capture.done();
                    }
                });

                // fail if capture is set but event is received for the non-captured target
                on_event(target1, "pointerover", function (event) {
                    log("pointerover", target1);
                    if(isPointerCapture == true) {
                        test(function() {
                            assert_unreached("pointerover shouldn't trigger for this target when capture is enabled");
                        }, "pointerover shouldn't trigger for the purple rectangle while the black rectangle has capture");
                    }
                });

                on_event(target1, "pointerout", function (event) {
                    log("pointerout", target1);
                    if(isPointerCapture == true) {
                        test(function() {
                            assert_unreached("pointerout shouldn't trigger for this target when capture is enabled");
                        }, "pointerout shouldn't trigger for the purple rectangle while the black rectangle has capture");
                    }
                });

                on_event(target1, "pointerenter", function (event) {
                    log("pointerenter", target1);
                    if(isPointerCapture == true) {
                        test(function() {
                            assert_unreached("pointerenter shouldn't trigger for this target when capture is enabled");
                        }, "pointerenter shouldn't trigger for the purple rectangle while the black rectangle has capture");
                    }
                });

                on_event(target1, "pointerleave", function (event) {
                    log("pointerleave", target1);
                    if(isPointerCapture == true) {
                        test(function() {
                            assert_unreached("pointerleave shouldn't trigger for this target when capture is enabled");
                        }, "pointerleave shouldn't trigger for the purple rectangle while the black rectangle has capture");
                    }
                });
            }
        </script>
        <h1>Pointer Events Capture Test</h1>
        <div id="complete-notice">
            <p>The following pointer types were detected: <span id="pointertype-log"></span>.</p>
        </div>
        <div id="log"></div>
    </body>
</html>